/*Copyright 2014 Rajesh Putta http://www.codesnippets4all.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* 
*/

package com.codesnippets4all.jthunder.core.framework.hierarchy;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.codesnippets4all.jthunder.core.config.handlers.LifeCycleConfig;
import com.codesnippets4all.jthunder.core.config.handlers.PhaseConfig;
import com.codesnippets4all.jthunder.core.config.handlers.TaskConfig;
import com.codesnippets4all.jthunder.core.exceptions.AutomationException;

/*
 * @author Rajesh Putta
 */
public class PhaseHierarchyManager {
	
	private LifeCycleConfig lifecycleConfig=null;
	
	private IHierarchy<PhaseConfig, Node> phaseHierarchy=new PhaseHierarchy();
	
	private Map<String, IHierarchy<TaskConfig, Node>> taskHierarchyMap=new HashMap<String, IHierarchy<TaskConfig, Node>>();
	
	
	public void setLifecycleConfig(LifeCycleConfig lifecycleConfig) {
		this.lifecycleConfig = lifecycleConfig;
	}
	
	public IHierarchy<PhaseConfig, Node> getHierarchy() {
		return phaseHierarchy;
	}
	
	public Map<String, IHierarchy<TaskConfig, Node>> getTaskHierarchyMap() {
		return taskHierarchyMap;
	}
	
	public void initialize()
	{
		if(this.lifecycleConfig==null)
			throw new AutomationException("Phase Hierarchy cannot be created without Lifecycle Config instance is set ...");
		
		this.createHierarchy();
	}
	
	public void createHierarchy()
	{
		Map<String, PhaseConfig> phaseConfigMap=this.lifecycleConfig.getPhaseConfigMap();

		this.phaseHierarchy.transform(phaseConfigMap);
		
		Set<Entry<String, PhaseConfig>> entrySet=phaseConfigMap.entrySet();
		
		for(Entry<String, PhaseConfig> entry:entrySet)
		{
			TaskHierarchy hierarchy=new TaskHierarchy();
			
			hierarchy.transform(entry.getValue().getConfig());
			
			taskHierarchyMap.put(entry.getKey(), hierarchy);
		}
	}
}
